/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobilevikings.app.simbalance;

import com.mobilevikings.app.Function;
import com.mobilevikings.app.LoadAlert;
import com.mobilevikings.app.api.InvalidResponseException;
import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

/**
 *
 * @author Jeroen
 */
public class SimBalanceFunction extends Function implements Runnable {

    private Form balanceForm;
    private SimBalance simBalance;
    private LoadAlert mAlrtRetrieving;
    private Alert mAlrtRetrievingFailed;

    public SimBalanceFunction(Display display) {
        super(display);
        simBalance = null;
        mAlrtRetrieving = new LoadAlert(mProperties.get("SIMBALANCE"), mProperties.get("SIMBALANCE_BUSY"), this);
        mAlrtRetrievingFailed = new Alert(mProperties.get("SIMBALANCE_GET_FAILED"), "", null, AlertType.ERROR);
    }

    public void show() {
        mDisplay.setCurrent(mAlrtRetrieving);
        mCancelled = false;
        Thread t = new Thread(this);
        t.start();
    }

    public void commandAction(Command c, Displayable d) {
        if(c == mCmdPrevious) {
            mDisplay.setCurrent(mPreviousScreen);
        }
    }

    public void run() {
        if (simBalance == null) {
            try {
                simBalance = mNetlogConnection.getSimBalance();
            } catch (InvalidResponseException ex) {
                mAlrtRetrievingFailed.setString(mProperties.get("ERROR_INVALIDRESPONSE") + ":\n" + ex.getDetails());
            } catch (IOException ex) {
                mAlrtRetrievingFailed.setString(mProperties.get("ERROR_IO"));
            }
        }
        if (simBalance == null) {
            mDisplay.setCurrent(mAlrtRetrievingFailed, mPreviousScreen);
        }
        else {
            initBalanceForm();
            mDisplay.setCurrent(balanceForm);
        }
    }

    private void initBalanceForm() {
        balanceForm = new Form(mProperties.get("SIMBALANCE"));
        balanceForm.append(new StringItem(mProperties.get("SIMBALANCE_CREDITS"),String.valueOf(simBalance.getCredits())));
        balanceForm.append(new StringItem(mProperties.get("SIMBALANCE_SMS"),String.valueOf(simBalance.getSms())));
        balanceForm.append(new StringItem(mProperties.get("SIMBALANCE_DATA"),String.valueOf(simBalance.getDataHumanReadable())));
        balanceForm.append(new StringItem(mProperties.get("SIMBALANCE_VALID_UNTIL"),simBalance.getExpirationDate()));
        String expired;
        if(simBalance.isExpired()) {
            expired = mProperties.get("YES");
        }
        else {
            expired = mProperties.get("NO");
        }
        balanceForm.append(new StringItem(mProperties.get("SIMBALANCE_EXPIRED"),expired));
        balanceForm.setCommandListener(this);
        balanceForm.addCommand(mCmdPrevious);
    }
}
